﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace NotificationService.DAL
{
    class PhoneDAL
    {
        DataAccessHelper db;
        public PhoneDAL()
        {
            db = new DataAccessHelper();
        }

        public bool InsertPhone(string deviceId, string url)
        {
            int count = 0;
            if (!CheckDeviceId(deviceId))
            {
                count = db.ExecuteNonQuery(@"Insert tblPhone (DeviceId, Url, LastTime) Values ('" + deviceId + "','" + url + "','" + DateTime.Now + "')");
            }
            else
            {
                count = db.ExecuteNonQuery(@"Update tblPhone Set Url = '" + url + "', LastTime = '" + DateTime.Now + "' Where DeviceId = '" + deviceId + "'");
            }
            return count > 0;
        }

        public bool CheckDeviceId(string deviceId)
        {
            int count = (int)db.ExecuteScalar("Select count(DeviceId) From tblPhone Where DeviceId = '" + deviceId + "'");
            return count > 0;
        }

        public SqlDataReader GetAllUrl()
        {
            SqlDataReader reader = db.ExecuteReader("Select Url From tblPhone");
            return reader;             
        }
    }
}